﻿using System;

namespace PicServer.BLL
{
    /// <summary>
    /// 文件的唯一索引
    /// 用5byte表示，前1byte表示数据表的序号；后4byte表示数据库记录的序号
    /// </summary>
    internal struct FileIndex
    {
        #region "字段"

        /// <summary>
        /// 文件的序号
        /// </summary>
        private Int64 fileUnicode;

        /// <summary>
        /// 对应数据库表的序号
        /// </summary>
        private Int32 tableIndex;

        /// <summary>
        /// 对应数据库记录的主键值
        /// </summary>
        private Int32 recordIndex;

        #endregion

        #region "属性"

        /// <summary>
        /// 获取文件的唯一索引
        /// </summary>
        public Int64 FileUnicode
        {
            get
            {
                Byte[] tableIndexByte = BitConverter.GetBytes(tableIndex);
                Byte[] recordIndexByte = BitConverter.GetBytes(recordIndex);
                Byte[] fileIndexByte = new Byte[8];
                Buffer.BlockCopy(recordIndexByte, 0, fileIndexByte, 0, 4);
                Buffer.BlockCopy(tableIndexByte, 0, fileIndexByte, 4, 1);
                return BitConverter.ToInt64(fileIndexByte, 0);
            }
        }

        /// <summary>
        /// 文件对应表的序号
        /// </summary>
        public Int32 TableIndex
        {
            get { return this.tableIndex; }
        }

        /// <summary>
        /// 文件对应表记录中的序号
        /// </summary>
        public Int32 RecordIndex
        {
            get { return this.recordIndex; }
        }
        
        #endregion

        public FileIndex(Int64 fileIndex)
        {
            this.fileUnicode = fileIndex;
            Byte[] fileIndexByte = BitConverter.GetBytes(fileIndex);
            this.recordIndex = BitConverter.ToInt32(fileIndexByte, 0);
            this.tableIndex = BitConverter.ToInt32(fileIndexByte, 4);
        }

        public FileIndex(Int32 tableIndex, Int32 recordIndex)
        {
            this.fileUnicode = 0;
            this.tableIndex = tableIndex;
            this.recordIndex = recordIndex;
        }
    }
}
